package timing.ukulele.portal.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import timing.ukulele.data.portal.view.SysParamVO;
import timing.ukulele.portal.convertor.ParamConvertor;
import timing.ukulele.portal.mapper.SysParamMapper;
import timing.ukulele.portal.persistent.SysParam;

import java.util.ArrayList;
import java.util.List;

@Service
public class SysParamService extends BaseOperatorService<SysParamMapper, SysParam> {
    public IPage<SysParamVO> getPage(Integer type, String value, String key, String remark, Long tenantId, Boolean deleted, int current, int size) {
        Page<SysParam> page = new Page<>(current, size);
        LambdaQueryChainWrapper<SysParam> query = this.lambdaQuery();
        query.eq(type != null, SysParam::getType, type);
        query.like(StringUtils.hasText(value), SysParam::getValue, value);
        query.like(StringUtils.hasText(key), SysParam::getKey, key);
        query.like(StringUtils.hasText(remark), SysParam::getRemark, remark);
        query.eq(tenantId != null, SysParam::getTenantId, tenantId);
        query.eq(deleted != null, SysParam::getDeleted, deleted);
        query.orderByDesc(SysParam::getId);
        IPage<SysParam> iPage = query.page(page);
        Page<SysParamVO> voPage = new Page<>(current, size);
        if (iPage != null && !CollectionUtils.isEmpty(iPage.getRecords())) {
            List<SysParamVO> voList = ParamConvertor.INSTANCE.p2vList(iPage.getRecords());
            voPage.setRecords(voList);
            voPage.setTotal(iPage.getTotal());
        }
        return voPage;
    }
}
